
	/************************************************************************
	*	Keypad Test Code [Includes test of serial]
	*	
	*	@brief	- integrates both the keypad and serial for a stubbed version of the user interface
	*
	*	@use	- to use this without the keypad need to put 5V on PORTB.1 to trigger the interrupt
	*			- the bit pattern on PORTB&&0b01111000 will then be read. Pattern corresponds to the pressed key.
	*			- num2key --> '1','4','7','*','2','5','8','0','3','6','9','#','A','B','C','D'

	*	@additional comments
	*			- note that for Janek's serial the strings MUST be stored as a variable so that they have an address
	*			- strings need to include the newline character '\n' and the CR '\r' if you want these
	*			- FIX - note that the serial routines are mislabled: curently called "hisr" whist actualy 'lisr'
	*
	*	@author		Felix Ossig-Bonanno
	*	@file       main.c
	*	@ver		1.0 (initial keypad release)
	*
	*************************************************************************/


#include <p18f452.h>
#include <stdio.h>
#include "configReg.h"		//set configuration bits
//#include "globals.c"		//create globals
#include "hisr_keypad.h"
#include "serial.h"

///////////////////////////////////////////////////////
//  Global Variables
///////////////////////////////////////////////////////
	//Create a structure 4x1bit reflecting the current operation states
	//typedef union
	//{
		struct
		{
			unsigned user 		: 1;
			unsigned metric 	: 1;
			unsigned single	 	: 1;
			unsigned continuous	: 1;
			unsigned celcius 	: 1;
		}Stateflags;
	//}
	
	//Create a structure 4x1bit reflecting the current display state
	//Only 1 bit should be high at a time; reflecting what is to be displayed on the LCD display
	struct
	{
		unsigned mode 		: 1;
		unsigned dist	 	: 1;
		unsigned temp	 	: 1;
		unsigned range_rds 	: 1;
		unsigned stats	 	: 1;
	}Dispflags;
	
	int global_samples_per_measurment = 5;
	int global_time_between_samples = 5;
	int global_range_gate = 5;
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
//  Function Prototypes
///////////////////////////////////////////////////////
void hisr(void);
void lisr(void);
void init_user(void);
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////


///////////////////////////////////////////////////////
//  Interupt Prototypes
///////////////////////////////////////////////////////
#pragma code high_vector=0x0008
void high_interrupt(void)
	{
		_asm GOTO hisr _endasm
	}
#pragma code
#pragma code low_vector=0x0018
void low_interrupt(void)
	{
		_asm GOTO lisr _endasm
	}
#pragma code
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////


void main (void)
{
	
	char messageDD[] = "Display Distance\n\r";
	char messageGD[] = "GET DISTANCE\n\r";
	
	char messageDT[] = "Display Temperature\n\r";
	char messageGT[] = "GET TEMP\n\r";
	
	char messageUM[] = "User Mode\n\r";
	char messageFM[] = "Factory Mode\n\r";


	init_keypad();				//init keypad
	init_user();				//setup user mode
	Serial_Setup();				//init serial
	Serial_Setup_Interrupt();	//init serial interrupt
	//stdout = _H_USART;		//remains of experiment to use fprintf as serial out	
	
	//General interrupt enable
	RCONbits.IPEN = 1;			//enable priorities
	INTCONbits.GIE = 1;			//enable global
	INTCONbits.PEIE = 1;		//enable perepheral
	
	while(1)
	{
		
		
		if (Dispflags.dist)
		{
			if(Stateflags.continuous || Stateflags.single)
			{
				Stateflags.single = 0;
				Serial_AppendString (messageGD);
			}
			
			Serial_AppendString (messageDD);
			
		}
		else if (Dispflags.temp)
		{
			if(Stateflags.continuous || Stateflags.single)
			{
				Serial_AppendString (messageGT);
				Stateflags.single = 0;
			}
				
			Serial_AppendString (messageDT);	
		}
		else if(Dispflags.mode)
		{
			
			if(Stateflags.user)
				Serial_AppendString (messageUM);
			else
				Serial_AppendString (messageFM);
		}
	}
}



#pragma interrupt hisr
void hisr(void)
{
	hisr_keypad();
}

#pragma interruptlow lisr
void lisr(void)
{
	hisr_tx232();	//FIX - mislabled
}
